import axios from 'axios';
import { Message } from 'element-ui';

let instance = axios.create({
    timeout: 20000,
    headers: {
        'X-Requested-With': 'XMLHttpRequest',
        'Content-Type': 'application/json',
    },
    withCredentials: false
});

// 请求前拦截
instance.interceptors.request.use(
    config => {
        return config
    },
    err => {
        Message.error({ message: '请求超时!' })
        return Promise.resolve(err)
    }
);

// 返回后拦截
instance.interceptors.response.use(
    data => {
        if (data.data.status === 200) {
            return data.data
        } else {
            Message.error({ message: data.data.message })
            return data.data
        }
    },
    err => {
        if (err.response) {
            if (err.response.status === 400) {
                Message.error({ message: err.response.data.message })
            } else if (err.response.status === 504 || err.response.status === 404) {
                Message.error({ message: '服务器被吃了⊙﹏⊙∥' })
            } else if (err.response.status === 401) {
                Message.error({ message: '登录信息失效，请重新登录' });
                // window.location.href = window.location.href.replace(/#\/.*/, '#/login');
                window.location.href = location.protocol + '//' + location.host + '/#/login';
            } else if (err.response.status === 500) {
                Message.error({ message: '服务器开小差了⊙﹏⊙∥' })
            } else {
                Message.error({ message: '网络不给力，请稍后再试' })
            }
        } else {
            Message.error({ message: '网络不给力，请稍后再试' })
        }
        return Promise.reject(err)
    }
)

let base = '';

const postJSON = (url, params) => {
    return instance.request({
        method: 'post',
        url: `${base}${url}`,
        data: params,
        headers: {
            'Content-Type': 'application/json;charset=utf-8'
        }
    })
}

const postToken = (url, params) => {
    return instance.request({
        method: 'post',
        url: url,
        data: params,
        headers: {
            'Content-Type': 'application/json;charset=utf-8',
            'Authorization': 'token'
        }
    })
}

const postRequest = (url, params) => {
    return instance.request({
        method: 'post',
        url: `${base}${url}`,
        data: bodyToParm(params),
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8',
        }
    })
}

const post = (url, params) => {
    return instance.request({
        method: 'post',
        url: url,
        data: bodyToParm(params),
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8',
        }
    })
}

const getRequest = (url, paramsBody) => {
    return instance.request({
        method: 'get',
        url: `${base}${url}${this.bodyToParm(paramsBody)}`,
    })
}

const multipleRequest = function (requestArray, callback) {
    axios.all(requestArray).then(axios.spread(callback))
};

// 对象型参数转param型参数
function bodyToParm(body, head, oldParam) {
    let param = '';
    let newHead = '';
    if (head) {
        newHead = head;
    }
    if (oldParam) {
        param = oldParam;
    }
    if (undefined !== body) {
        let flag = 0;
        if (param) {
            flag = 1;
        }
        for (let i in body) {
            if (undefined !== body[i]) {
                if ((typeof body[i]) === 'string') {
                    body[i] = body[i].trim();
                }
                if (flag === 0) {
                    if (body[i] instanceof Object) {
                        param = bodyToParm(body[i], `${newHead}${i}.`, param);
                    } else {
                        param = `?${newHead}${i}=${encodeURIComponent(body[i])}`;
                    }
                    if (param) {
                        flag = 1;
                    }
                } else {
                    if (body[i] instanceof Object) {
                        param = bodyToParm(body[i], `${newHead}${i}.`, param);
                    } else {
                        param = `${param}&${newHead}${i}=${encodeURIComponent(body[i])}`;
                    }
                }
            }
        }
    }
    return param;
}

export { getRequest, postRequest, postJSON, post, postToken, multipleRequest }
